package com.group.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.group.pojo.OrderReport;
import com.group.service.ProductOrderService;
import com.group.service.ProductService;
import com.group.utils.R;
import io.swagger.annotations.Api;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * @author: 如寄
 * @version: v1.0
 * @description: com.group.controller
 * @date:2021/9/15
 */
@RestController
@Api
@CrossOrigin
public class CommonController {
    @Autowired
    private FastFileStorageClient storageClient;
    @Autowired
    private ProductOrderService productService;

    @PostMapping("/uploadImg")
    public R addImg(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("上传文件是否为空：" + file.isEmpty());
        InputStream is = null;
        if (file != null) {
            // 获取文件流
            is = file.getInputStream();
            // 进行文件上传
            StorePath storePath = storageClient.uploadFile(is, file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
            // 获得文件上传后访问地址
            String fullPath = "http://192.168.14.129/" + storePath.getFullPath();

            return R.ok().data("path", fullPath);
        } else {
            System.out.println("没有找到相对应的文件");
        }
        return R.error();
    }

    @GetMapping("/report")
    public R report() {
        List<OrderReport> reports = productService.getReport();
        return R.ok().data("report",reports);
    }
}
